import path from 'path';
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { visualizer } from 'rollup-plugin-visualizer';
import ElementPlus from "unplugin-element-plus/vite"

import OptimizationPersist from 'vite-plugin-optimize-persist'
import PkgConfig from 'vite-plugin-package-config'
import viteCompression from 'vite-plugin-compression';

// 按需引入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// mock
import { viteMockServe } from 'vite-plugin-mock';

//引入cesium插件
import cesium from 'vite-plugin-cesium';

// vite.config.ts
export default defineConfig({
  base: "/",
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
      "~": path.resolve(__dirname, "public"),
      "/icon": "./src/assets/"
    },
  },
  build: {
    assetsDir: './static',
    chunkSizeWarningLimit: 500,
    cssCodeSplit: true,
    minify: 'terser',
    terserOptions: {
      compress: {
        // warnings: false,
        drop_console: true,  //打包时删除console
        drop_debugger: true, //打包时删除 debugger
        pure_funcs: ['console.log'],
      },
      output: {
        // 去掉注释内容
        comments: true,
      },
    },
    rollupOptions: {
      output: {
        manualChunks: {
          // 拆分代码，这个就是分包，配置完后自动按需加载，现在还比不上webpack的splitchunk，不过也能用了。
          vue: ['vue'],
          echarts: ['echarts'],
          "element-plus": ['element-plus'],
        },
      },
    },
  },
  server: {
    // 是否自动打开浏览器
    open: true,
    // 服务器主机名，如果允许外部访问，可设置为"0.0.0.0"
    host: '0.0.0.0',
    // 服务器端口号
    port: 3000,
    middlewareMode: 'html',
    // proxy:{
    //   "/api":{
    //     target:"http://jsonplaceholder.typicode.com",
    //     changeOrigin:true,
    //     rewrite:()=>path.replace(/^\/api/,"")
    //   }
    // }
  },
  css: {
    preprocessorOptions: {
      scss: {
        charset: false,
        additionalData: `@use "@/assets/styles/index.scss" as *;`,
      },
    },
  },
  plugins: [
    // PkgConfig(),
    // OptimizationPersist(),
    vue(),
    cesium({
      // rebuildCesium: true //Default copy min cesium file to dist. if true will rebuild cesium from source.
    }),
    viteMockServe({
      // default
      mockPath: 'mock',
      // localEnabled: command === 'serve',
    }),
    visualizer(),
    // 打包压缩，主要是本地gzip，如果服务器配置压缩也可以
    // viteCompression(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    // use unplugin-vue-components
    // Components({
    //   resolvers: [
    //     ElementPlusResolver({
    //       importStyle: "sass",
    //       // directives: true,
    //       // version: "2.1.5",
    //     }),
    //   ],
    // }),
    // 或者使用 unplugin-element-plus
    // ElementPlus(),
  ],
  define: {
    // __APP_VERSION__: JSON.stringify('koala-bear'),
    // global: 'globalThis'，
    __TRUE__: 'false',
  },
  // optimizeDeps: {
  //   exclude: ['feathers-vuex > fast-copy']
  // },
  // optimizeDeps: {
  //   include: ["./preCompiles"]
  // }
})